//This function checks the radio button selected 
	function getCheckedRadio(radiobtnname) {
		var selectedval = "";
        var radioButtons = document.getElementsByName(radiobtnname);
        for (var x = 0; x < radioButtons.length; x ++) {
          if (radioButtons[x].checked) {
          selectedval = radioButtons[x].value;
            //alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
          }
        }
        return selectedval;
	}

function getComboSelected(fieldname) {

	//var oForm = eval('document.' + getFormName());
	//alert(fieldname);
	var selectedval = document.getElementsByName(fieldname);
	//alert('Selected Index: ' + oField.selectedIndex);
	
	var oField = eval('document.' + getFormName() + '.' + fieldname);
	//alert(oField);
	var len = oField.length;
	//alert(len);
	//alert('Form Name: ' + getFormName());
	var i = 0;
	var chosen = "none";
	
	for (i = 0; i < len; i++) {
	   var obj = eval('document.' + getFormName() + '.' + fieldname + '[' + i + ']');
		if (obj.selected) {
			chosen = obj.value;
		} 
	}
	//alert('Chosen Value: ' + chosen);
	return chosen;

	
}

function changeGrade() {
    var select = document.getElementById("selectgrade");
    var selected = select.options[select.selectedIndex].value;
	$('#mask').hide();
	$('.window').hide();
	//$("#dialog").dialog("close");
	location.href = '/lwf/home.do?action=changegrade&chosengrade=' + selected;
	return;	
	
	
}

   function submitForm(action) {
	   //alert('submitForm - 1');
      if(action == null || action.length == 0) {
    	  //alert('submitForm - 2');
         document.forms[0].submit();
      }
      else {
    	  //alert('submitForm - 3');
         oForm = eval("document." + getFormName());
         oForm.action = '/lwf' + action;
         //alert(oForm.action);
         oForm.method = 'POST';
         oForm.submit();
      }
      return;
   }

   function getFormName() {
      return document.forms[0].name;
   }
   
   function checkForElement(formName,elementType){
      var elemSize = eval('document.' + getFormName() + '.elements.length');
      var isThere = 0;
      var i;var el;
      for (i=0;i<elemSize;i++) {
         el = eval('document.' + getFormName() + '.elements[i]');
         if(el.type  == elementType) {
            return true;
         }
      }
      return false;
   }
   
   function getCurrentBrowserName() {
	   var browserName = "";

	   var ua = navigator.userAgent.toLowerCase();
	   if ( ua.indexOf( "opera" ) != -1 ) {
		   browserName = "opera";
	   } 
	   else if ( ua.indexOf( "msie" ) != -1 ) {
		   browserName = "msie";
	   } 
	   else if ( ua.indexOf( "safari" ) != -1 ) {
		   browserName = "safari";
	   } 
	   else if ( ua.indexOf( "mozilla" ) != -1 ) {
		   if ( ua.indexOf( "firefox" ) != -1 ) {
			   	browserName = "firefox";
	       } 	
	       else {
	    	   browserName = "mozilla";
	       }
	   }
	   else {
		   browserName = "";
	   }

	   return browserName;
   }
   
   function tellmemybrowser() {
	   alert(navigator.appName);
   } 
   
   function getOperatingSystem() {
	   var OSName = navigator.appVersion;
	   
	   if (navigator.appVersion.indexOf("Win")!=-1) {
		   OSName="Windows";
	   }
	   else if (navigator.appVersion.indexOf("Mac")!=-1) {
		   OSName="MacOS";
	   }
	   else if (navigator.appVersion.indexOf("X11")!=-1) {
		   OSName="UNIX";
	   }
	   else if (navigator.appVersion.indexOf("Linux")!=-1) {
		   OSName="Linux";
	   }
	   
	   return OSName;
	   
   }
   
   function trimSpaces(strText) {
      //This will get rid of leading spaces
      while(strText.substring(0,1) == ' ')
      	strText = strText.substring(1, strText.length);
      //This will get rid of trailing spaces	
      while(strText.substring(strText.length-1, strText.length) == ' ')
      	strText = strText.substring(0, strText.length-1);
      return strText;		
   }
   
   function isEmpty(str) {
		
	    //alert('Checks for isEmpty method');
		//alert(' For null value: isEmpty returns: ' + isEmpty(null)); //returns true
		//alert(' For NOT null value: isEmpty returns: ' + isEmpty('hello'));	//returns false
		//alert(' For empty (just two quotes) value: isEmpty returns: ' + isEmpty(''));	//returns true
		//alert(' For empty (just space) value: isEmpty returns: ' + isEmpty(' '));	//returns false
	    
	    return (!str || 0 === str.length);
   }

   function isBlank(str) {
	   
		//alert('Checks for isBlank method');
		//alert(' For null value: isBlank returns: ' + isBlank(null)); //returns true
		//alert(' For NOT null value: isBlank returns: ' + isBlank('hello'));	//returns false
		//alert(' For empty (just two quotes) value: isBlank returns: ' + isBlank(''));	//returns true
		//alert(' For empty (just space) value: isBlank returns: ' + isBlank(' '));	//returns true
	    
	    return (!str || /^\s*$/.test(str));
   }
 

   function isElementExists(elementName){
      //var elemSize = eval('document.' + formName + '.elements.length');
      var elemSize = eval('document.' + getFormName() + '.elements.length');
      var i;var el;
      for (i=0;i<elemSize;i++) {
         //el = eval('document.' + formName + '.elements[i]');
         el = eval('document.' + getFormName() + '.elements[i]');
         //alert(el.name);
	      if(el.name == elementName) {
	            return true;
	      }
      }
      return false;
   } 
   
   function underDevelopment() {
	   //alert('CHeck...........');
      alert('This feature is under development....');
   }  
   
   /*
     Attributes
     ==========
		menubar     - Specifies whether or not to display a menu bar at the top of the window.  
        		      Value = yes or no, 1 or 0.
 
		toolbar     - Specifies whether or not to display the main toolbar (with the back, forward, stop etc. buttons).  
		              Value = yes or no, 1 or 0.
 
		location    - Specifies whether or not to display the location bar (the Address Bar in Internet Explorer) - where URLs are typed and displayed. 
		               Value = yes or no, 1 or 0.
 
		directories - Specifies whether or not to display any additional toolbar (e.g the Links Bar in Internet Explorer). 
		              Value = yes or no, 1 or 0.
 
		status      - Specifies whether or not to display the status bar at the foot of the window. 
		              Value = yes or no, 1 or 0.
 
		scrollbars  - Specifies whether or not to display the horizontal and vertical scrollbars that normally appear when the page content is larger than the screen. 
		              Value = yes or no, 1 or 0.
 
		height      - Specifies the height of the window in pixels. 
		              Value = number
 
		width       - Specifies the width of the window in pixels. 
		              Value = number
 
		left        - Specifies the distance in pixels of the new window from the left edge of the screen. (This applies to Internet Explorer. For Netscape Navigator use screenX). 
		              Value = number
 
		top         - Specifies the distance in pixels of the new window from the top edge of the screen. (This applies to Internet Explorer. For Netscape Navigator use screenY). 
		              Value = number
 
		resizable   - When enabled, allows the user to manually resize the window by dragging its edges or corners. 
		              Value = yes or no, 1 or 0.
 
		fullscreen  - When enabled causes the window to open in full-screen mode (Internet Explorer only.) 
		              Value = yes or no, 1 or 0.
    */
   function popup(url, name, attributes) {
       window.open(url, name, attributes);
   }
   
   function popupfull(url) {
       var attributes = 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,fullscreen=no';
       attributes = attributes + ',width=' + (screen.availWidth - 10);
       attributes = attributes + ',height=' + (screen.availHeight - 105);
       attributes = attributes + ',screenX=0,screenY=0,left=0,top=0';
       
       //alert('attributes: ' + attributes);
       //alert('width=' + screen.availWidth + '  height=' + screen.availHeight);
       //window.open(url, name, attributes);
       var wOpen;
       wOpen = window.open('', '', attributes);
       wOpen.location = url;
       wOpen.focus();
       wOpen.moveTo(0,0);
       wOpen.resizeTo(screen.availWidth, screen.availHeight);
       
       return wOpen;
       
   }
   
   //------ For submit button
   
   function hotKeys(action) { 
	   alert(action);
       if( !e ) {
           if( window.event ) {
               //DOM
               e = window.event;
           } else {
               //TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
               return;
           }
       }
       
       if( typeof( e.which ) == 'number' ) {
           //NS 4, NS 6+, Mozilla 0.9+, Opera
           alert('1--> NS 4, NS 6+, Mozilla 0.9+, Opera');
           e = e.which;
       } else if( typeof( e.keyCode ) == 'number' ) {
           //IE, NS 6+, Mozilla 0.9+
           alert('2--> IE, NS 6+, Mozilla 0.9+');
           e = e.keyCode;
       } else if( typeof( e.charCode ) == 'number' ) {
           //also NS 6+, Mozilla 0.9+
           alert('3-->  NS 6+, Mozilla 0.9+');
           e = e.charCode;
       } else {
           //TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
           alert('TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE');
           return;
       }
       //window.alert('The key pressed has keycode ' + e + ' and is key ' + String.fromCharCode( e ) );
       if(e == 13) {
           alert('Enter key is pressed AND going to the next screen!');
           if(action != null) {
        	   submitForm(action);
           }
           
       }

   } 
   
   function checkSubmit(e, action) {
	   //alert('checkSubmit - 1');
       if(e && e.keyCode == 13) {
    	   //alert('checkSubmit - 2');
    	   submitForm(action);

          //document.forms[0].submit();
       }
    }
   
   function submitPreferences(e, action) {
	   //alert(e.keyCode);
	   if(e && e.keyCode == 13) {
		   var operation = getCheckedRadio("preferences.level");
		   if(operation == '') {
			   alert('Select preferences please......');
		   }
		   else {
			   submitForm(action);
		   }		   
	   }
   }
   
   function submitPreferences1(action) {
	  // alert('submitPreferences1: ');
	   var operation = getCheckedRadio("preferences.level");
	   if(operation == '') {
		   alert('Select preferences please......');
	   }
	   else {
	   	  submitForm(action);
	   }		   
   }
   
   function guestLogin(action) {
	   var selectedGrade = trimSpaces(getComboSelected('selectedGrade'));
	   if(selectedGrade.length != 0) {
		   submitForm(action);
	   }
	   else {
		   alert('Please select grade');
	   }
   }
   
   function setFocus(name) {
	   //alert('Focus On: ') + name;
	   document.getElementById(name).focus();
	   
//	   var focusControl = document.forms["haform"].elements["btnSubmit"];
//
//	   if (focusControl.type != "hidden" && !focusControl.disabled) {
//	      focusControl.focus();
//	   }

   }
   
	function getWorkSheets() {

		var selectedgrade = getComboSelected('selectedGrade');
		selectedgrade = (isBlank(selectedgrade)) ? '' : trimSpaces(selectedgrade);
		var selectedsubject = getComboSelected('selectedSubject');
		selectedsubject = (isBlank(selectedsubject)) ? '' : trimSpaces(selectedsubject);
		//alert('Grade: ' + selectedgrade + ', Subject: ' + selectedsubject);
		if(!isEmpty(selectedgrade) && !isEmpty(selectedsubject)) {
			//alert('Submitting Form');
			submitForm('/worksheet.do?action=home');
		}

	}
	
	//----------------------For Printing worksheets
	var theChild;
	function showPdf(url) {
		if(theChild!=null) {
			if(getCurrentBrowserName() != 'msie') {
				theChild.close();
			}
		}
		theChild = popupPdf(url);
		theChild.focus();
	} 
   
   function popupPdf(url) {
	   alert('Attention: The document will be opened in another window. Please close window after printing or viewing!');
       var attributes = 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,fullscreen=no';
       attributes = attributes + ',width=' + (screen.availWidth - 150);
       attributes = attributes + ',height=' + (screen.availHeight - 250);
       attributes = attributes + ',screenX=50,screenY=50,left=10,top=10';
       
       //alert('attributes: ' + attributes);
       //alert('width=' + screen.availWidth + '  height=' + screen.availHeight);
       //window.open(url, name, attributes);
       var wOpen;
       wOpen = window.open(url, 'theChild', attributes);
       
       //wOpen.focus();
       wOpen.moveTo(75,50);
       wOpen.resizeTo(screen.availWidth, screen.availHeight);
       
       return wOpen;
       
   }
   
   function goBack() {
	   window.history.back();
   }
   
   function closeMe() {
	   window.close();
   }
   
   
   function underDevelopment() {
	   alert('Under development!');
   }
   
   function askLogin() {
	   alert('Please login to the application to avail the features');
   }   
   
   function contact() {
	   alert('Email: info@learnwithfun.net');
   }

   
